package api

import (
	"cgs-server/helper"
	"cgs-server/server"
	"cgs-server/server/base"
	"cgs-server/server/cgs/app/models"
	"cgs-server/server/cgs/app/models/dto"
	"cgs-server/server/cgs/app/service"
	"cgs-server/server/cgs/app/store"
	"go.mongodb.org/mongo-driver/bson"
	"net/http"
)

type WorksheetRowsHandler struct {
	// srv service
	srv service.Service
	// store
	store store.Factory
}

func NewWorksheetRowsHandler(store store.Factory) *WorksheetRowsHandler {
	return &WorksheetRowsHandler{
		srv:   service.NewService(store),
		store: store,
	}
}

func (wg *WorksheetRowsHandler) Get(w http.ResponseWriter, r *http.Request) {
	rowId := r.FormValue("rowId")
	worksheetId := r.FormValue("worksheetId")
	if rowId == "" || worksheetId == "" {
		_, _ = helper.WriteJSON(w, server.Fail("rowId and worksheetId is required"))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	row, err := wg.srv.WorksheetRows().Get(rowId, worksheetId, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK().WithData(row))
}

func (wg *WorksheetRowsHandler) Delete(w http.ResponseWriter, r *http.Request) {
	rowId := r.FormValue("rowId")
	worksheetId := r.FormValue("worksheetId")
	if rowId == "" || worksheetId == "" {
		_, _ = helper.WriteJSON(w, server.Fail("rowId and worksheetId is required"))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	err = wg.srv.WorksheetRows().Delete(rowId, worksheetId, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK())
}

func (wg *WorksheetRowsHandler) Save(w http.ResponseWriter, r *http.Request) {
	var wsRow models.WorksheetRow
	err := helper.ReadRequestBody(&wsRow, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	err = wg.srv.WorksheetRows().Save(&wsRow, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK().WithData(bson.M{"id": wsRow.GetId()}))
}

func (wg *WorksheetRowsHandler) SavePublic(w http.ResponseWriter, r *http.Request) {
	var wsRow models.WorksheetRow
	err := helper.ReadRequestBody(&wsRow, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	err = wg.srv.WorksheetRows().SavePublic(&wsRow, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK().WithData(bson.M{"id": wsRow.GetId()}))
}

func (wg *WorksheetRowsHandler) Update(w http.ResponseWriter, r *http.Request) {
	var wsRow models.WorksheetRow
	err := helper.ReadRequestBody(&wsRow, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	err = wg.srv.WorksheetRows().Update(r.Context(), &wsRow, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK())
}

func (wg *WorksheetRowsHandler) List(w http.ResponseWriter, r *http.Request) {
	param := &models.FilterParam{}
	err := helper.ReadRequestBody(param, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	if param.WorksheetId == "" {
		_, _ = helper.WriteJSON(w, server.Fail("worksheetId is required"))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	result, err := wg.srv.WorksheetRows().List(param, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, result)
}

func (wg *WorksheetRowsHandler) BatchSave(w http.ResponseWriter, r *http.Request) {
	var wsRow models.WorksheetRows
	err := helper.ReadRequestBody(&wsRow, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	err = wg.srv.WorksheetRows().BatchAddRow(&wsRow, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK())
}

func (wg *WorksheetRowsHandler) BatchUpdate(w http.ResponseWriter, r *http.Request) {
	var wsRow dto.BatchUpdateRow
	err := helper.ReadRequestBody(&wsRow, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	err = wg.srv.WorksheetRows().BatchUpdateRow(&wsRow, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK())
}

func (wg *WorksheetRowsHandler) BatchDelete(w http.ResponseWriter, r *http.Request) {
	var wsRow dto.BatchUpdateRow
	err := helper.ReadRequestBody(&wsRow, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	err = wg.srv.WorksheetRows().BatchDeleteRow(&wsRow, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK())
}
